﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


public partial class _index : System.Web.UI.Page
{
    private string voteID1 = "1";
    private string voteID2 = "2";
    private string voteID3 = "3";
    private string voteID4 = "4";
    private string voteID5 = "5";
    private string voteID6 = "6";
    private string voteID7 = "7";
    private string voteID8 = "8";
    private string voteID9 = "9";
    private string voteID10 = "10";
    //private string voteID11 = "11";
    //private string voteID12 = "12";
    //private string voteID13 = "13";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //string userID;
            //userID = Request.QueryString["userID"];


            Response.Write("<script>alert('  您只能填一次问卷，谢谢！ 进入结果页面!'+userID);</script>");

            SqlConnection con = DB.createConnection();
            con.Open();
            SqlCommand cmd1 = new SqlCommand("select voteTitle from voteIssue where voteID=" + this.voteID1, con);
            SqlCommand cmd2 = new SqlCommand("select voteTitle from voteIssue where voteID=" + this.voteID2, con);
            SqlCommand cmd3 = new SqlCommand("select voteTitle from voteIssue where voteID=" + this.voteID3, con);
            SqlCommand cmd4 = new SqlCommand("select voteTitle from voteIssue where voteID=" + this.voteID4, con);
            SqlCommand cmd5 = new SqlCommand("select voteTitle from voteIssue where voteID=" + this.voteID5, con);
            SqlCommand cmd6 = new SqlCommand("select voteTitle from voteIssue where voteID=" + this.voteID6, con);
            SqlCommand cmd7 = new SqlCommand("select voteTitle from voteIssue where voteID=" + this.voteID7, con);
            SqlCommand cmd8 = new SqlCommand("select voteTitle from voteIssue where voteID=" + this.voteID8, con);
            SqlCommand cmd9 = new SqlCommand("select voteTitle from voteIssue where voteID=" + this.voteID9, con);
            SqlCommand cmd10 = new SqlCommand("select voteTitle from voteIssue where voteID=" + this.voteID10, con);
            //SqlCommand cmd11 = new SqlCommand("select voteTitle from voteIssue where voteID=" + this.voteID11, con);
            //SqlCommand cmd12 = new SqlCommand("select voteTitle from voteIssue where voteID=" + this.voteID12, con);
            //SqlCommand cmd13 = new SqlCommand("select voteTitle from voteIssue where voteID=" + this.voteID13, con);

            string title1 = Convert.ToString(cmd1.ExecuteScalar());
            string title2 = Convert.ToString(cmd2.ExecuteScalar());
            string title3 = Convert.ToString(cmd3.ExecuteScalar());
            string title4 = Convert.ToString(cmd4.ExecuteScalar());
            string title5 = Convert.ToString(cmd5.ExecuteScalar());
            string title6 = Convert.ToString(cmd6.ExecuteScalar());
            string title7 = Convert.ToString(cmd7.ExecuteScalar());
            string title8 = Convert.ToString(cmd8.ExecuteScalar());
            string title9 = Convert.ToString(cmd9.ExecuteScalar());
            string title10 = Convert.ToString(cmd10.ExecuteScalar());


            this.Label1.Text = "01、" + title1;
            this.Label2.Text = "02、" + title2;
            this.Label3.Text = "03、" + title3;
            this.Label4.Text = "04、" + title4;
            this.Label5.Text = "05、" + title5;
            this.Label6.Text = "06、" + title6;
            this.Label7.Text = "07、" + title7;
            this.Label8.Text = "08、" + title8;
            this.Label9.Text = "09、" + title9;
            this.Label10.Text = "10、" + title10;



         
            //查询对应的投票条目
            SqlCommand cmdItem1 = new SqlCommand("select votedetailID,voteItem from voteContent where voteID=" + this.voteID1, con);
            SqlDataReader sdr1 = cmdItem1.ExecuteReader();
            this.RadioButtonList1.DataSource = sdr1;
            this.RadioButtonList1.DataTextField = "voteItem";
            this.RadioButtonList1.DataValueField = "votedetailID";
            this.RadioButtonList1.DataBind();
            sdr1.Close();

            SqlCommand cmdItem2 = new SqlCommand("select votedetailID,voteItem from voteContent where voteID=" + this.voteID1, con);
            SqlDataReader sdr2 = cmdItem2.ExecuteReader();
            this.RadioButtonList2.DataSource = sdr2;
            this.RadioButtonList2.DataTextField = "voteItem";
            this.RadioButtonList2.DataValueField = "votedetailID";
            this.RadioButtonList2.DataBind();
            sdr2.Close();

            SqlCommand cmdItem3 = new SqlCommand("select votedetailID,voteItem from voteContent where voteID=" + this.voteID1, con);
            SqlDataReader sdr3 = cmdItem3.ExecuteReader();
            this.RadioButtonList3.DataSource = sdr3;
            this.RadioButtonList3.DataTextField = "voteItem";
            this.RadioButtonList3.DataValueField = "votedetailID";
            this.RadioButtonList3.DataBind();
            sdr3.Close();

            SqlCommand cmdItem4 = new SqlCommand("select votedetailID,voteItem from voteContent where voteID=" + this.voteID1, con);
            SqlDataReader sdr4 = cmdItem4.ExecuteReader();
            this.RadioButtonList4.DataSource = sdr4;
            this.RadioButtonList4.DataTextField = "voteItem";
            this.RadioButtonList4.DataValueField = "votedetailID";
            this.RadioButtonList4.DataBind();
            sdr4.Close();

            SqlCommand cmdItem5 = new SqlCommand("select votedetailID,voteItem from voteContent where voteID=" + this.voteID1, con);
            SqlDataReader sdr5 = cmdItem5.ExecuteReader();
            this.RadioButtonList5.DataSource = sdr5;
            this.RadioButtonList5.DataTextField = "voteItem";
            this.RadioButtonList5.DataValueField = "votedetailID";
            this.RadioButtonList5.DataBind();
            sdr5.Close();

            SqlCommand cmdItem6 = new SqlCommand("select votedetailID,voteItem from voteContent where voteID=" + this.voteID1, con);
            SqlDataReader sdr6 = cmdItem6.ExecuteReader();
            this.RadioButtonList6.DataSource = sdr6;
            this.RadioButtonList6.DataTextField = "voteItem";
            this.RadioButtonList6.DataValueField = "votedetailID";
            this.RadioButtonList6.DataBind();
            sdr6.Close();

            SqlCommand cmdItem7 = new SqlCommand("select votedetailID,voteItem from voteContent where voteID=" + this.voteID1, con);
            SqlDataReader sdr7 = cmdItem7.ExecuteReader();
            this.RadioButtonList7.DataSource = sdr7;
            this.RadioButtonList7.DataTextField = "voteItem";
            this.RadioButtonList7.DataValueField = "votedetailID";
            this.RadioButtonList7.DataBind();
            sdr7.Close();

            SqlCommand cmdItem8 = new SqlCommand("select votedetailID,voteItem from voteContent where voteID=" + this.voteID1, con);
            SqlDataReader sdr8 = cmdItem8.ExecuteReader();
            this.RadioButtonList8.DataSource = sdr8;
            this.RadioButtonList8.DataTextField = "voteItem";
            this.RadioButtonList8.DataValueField = "votedetailID";
            this.RadioButtonList8.DataBind();
            sdr8.Close();

            SqlCommand cmdItem9 = new SqlCommand("select votedetailID,voteItem from voteContent where voteID=" + this.voteID1, con);
            SqlDataReader sdr9 = cmdItem9.ExecuteReader();
            this.RadioButtonList9.DataSource = sdr9;
            this.RadioButtonList9.DataTextField = "voteItem";
            this.RadioButtonList9.DataValueField = "votedetailID";
            this.RadioButtonList9.DataBind();
            sdr9.Close();

            SqlCommand cmdItem10 = new SqlCommand("select votedetailID,voteItem from voteContent where voteID=" + this.voteID1, con);
            SqlDataReader sdr10 = cmdItem10.ExecuteReader();
            this.RadioButtonList10.DataSource = sdr10;
            this.RadioButtonList10.DataTextField = "voteItem";
            this.RadioButtonList10.DataValueField = "votedetailID";
            this.RadioButtonList10.DataBind();
            sdr10.Close();  

            con.Close();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = DB.createConnection();
            con.Open();

            String selectname=DropDownList1.SelectedIndex.ToString();
            SqlCommand cmd1 = new SqlCommand();
            cmd1.Connection = con;
            int voteID1Mark = Convert.ToInt32(this.RadioButtonList1.SelectedItem.ToString());
            int voteID2Mark = Convert.ToInt32(this.RadioButtonList2.SelectedItem.ToString());
            int voteID3Mark = Convert.ToInt32(this.RadioButtonList3.SelectedItem.ToString());
            int voteID4Mark = Convert.ToInt32(this.RadioButtonList4.SelectedItem.ToString());
            int voteID5Mark = Convert.ToInt32(this.RadioButtonList5.SelectedItem.ToString());
            int voteID6Mark = Convert.ToInt32(this.RadioButtonList6.SelectedItem.ToString());
            int voteID7Mark = Convert.ToInt32(this.RadioButtonList7.SelectedItem.ToString());
            int voteID8Mark = Convert.ToInt32(this.RadioButtonList8.SelectedItem.ToString());
            int voteID9Mark = Convert.ToInt32(this.RadioButtonList9.SelectedItem.ToString());
            int voteID10Mark = Convert.ToInt32(this.RadioButtonList10.SelectedItem.ToString());

            int userMarkSum = voteID1Mark + voteID2Mark + voteID3Mark + voteID4Mark + voteID5Mark + voteID6Mark + voteID7Mark + voteID8Mark + voteID9Mark + voteID10Mark;
            String userMarkAvg = Convert.ToString(userMarkSum/10)+'.'+Convert.ToString(userMarkSum%10);
            cmd1.CommandText = "update userMark set voteID1=voteID1+" + this.RadioButtonList1.SelectedItem.ToString() + ",voteID2=voteID2+" + this.RadioButtonList2.SelectedItem.ToString() + ",voteID3=voteID3+" + this.RadioButtonList3.SelectedItem.ToString() + ",voteID4=voteID4+" + this.RadioButtonList4.SelectedItem.ToString() + ",voteID5=voteID5+" + this.RadioButtonList5.SelectedItem.ToString() + ",voteID6=voteID6+" + this.RadioButtonList6.SelectedItem.ToString() + ",voteID7=voteID7+" + this.RadioButtonList7.SelectedItem.ToString() + ",voteID8=voteID8+" + this.RadioButtonList8.SelectedItem.ToString() + ",voteID9=voteID9+" + this.RadioButtonList9.SelectedItem.ToString() + ",voteID10=voteID10+" + this.RadioButtonList10.SelectedItem.ToString() + ",voteSum=voteSum+" + userMarkSum.ToString() + ",voteAvg=voteAvg+" + userMarkAvg.ToString() + "where userID=" + selectname;
            
            //更新数据库
            //int n = 1;
            //if ((cmd1.CommandText == "update voteContent set voteNum=voteNum+1 where voteID=1 and votedetailID="))
            //    n = 0;
            //else n = 1;
            //if (n == 0) { }
            //else
            //{
            //更新数据库
            cmd1.ExecuteNonQuery();
            //}

            //提交后直接清空
            RadioButtonList1.SelectedItem.Selected = false;
            RadioButtonList2.SelectedItem.Selected = false;
            RadioButtonList3.SelectedItem.Selected = false;
            RadioButtonList4.SelectedItem.Selected = false;
            RadioButtonList5.SelectedItem.Selected = false;
            RadioButtonList7.SelectedItem.Selected = false;
            RadioButtonList8.SelectedItem.Selected = false;
            RadioButtonList9.SelectedItem.Selected = false;
            RadioButtonList10.SelectedItem.Selected = false;

            this.RegisterClientScriptBlock("js", "<script>alert('  提交成功！  ');</script>");
            Response.Redirect("success.aspx");
            //Response.Write("<script>alert('  提交成功！  ')</script>");


        }
        catch
        {
            this.RegisterClientScriptBlock("js", "<script>alert('请填写完成再提交哦！');</script>");
        }
}
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            if (RadioButtonList1.SelectedItem == null)
            { }
            else
            {
                RadioButtonList1.SelectedItem.Selected = false;
            }
            if (RadioButtonList2.SelectedItem == null)
            { }
            else
            {
                RadioButtonList2.SelectedItem.Selected = false;
            }
            if (RadioButtonList3.SelectedItem == null)
            { }
            else
            {
                RadioButtonList3.SelectedItem.Selected = false;
            }
            if (RadioButtonList4.SelectedItem == null)
            { }
            else
            {
                RadioButtonList4.SelectedItem.Selected = false;
            }
            if (RadioButtonList5.SelectedItem == null)
            { }
            else
            {
                RadioButtonList5.SelectedItem.Selected = false;
            }
            if (RadioButtonList6.SelectedItem == null)
            { }
            else
            {
                RadioButtonList6.SelectedItem.Selected = false;
            }
            if (RadioButtonList7.SelectedItem == null)
            { }
            else
            {
                RadioButtonList7.SelectedItem.Selected = false;
            }
            if (RadioButtonList8.SelectedItem == null)
            { }
            else
            {
                RadioButtonList8.SelectedItem.Selected = false;
            }
            if (RadioButtonList9.SelectedItem == null)
            { }
            else
            {
                RadioButtonList9.SelectedItem.Selected = false;
            }
            if (RadioButtonList10.SelectedItem == null)
            { }
            else
            {
                RadioButtonList10.SelectedItem.Selected = false;
            }
            
            //TextBox1.Text = "  ";
            //TextBox2.Text = "  ";
            //TextBox3.Text = "  ";
        }
        catch
        {
            //Label14.Text = "";
        }


        Page_Load(sender, e);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("showResult.aspx?voteID=" + this.voteID1);
    }
}
